package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.model.UserInfo;
import com.lonty.intelligence.request.UserInfoAndCommunityList;
import com.lonty.intelligence.service.UserInfoService;
import com.lonty.intelligence.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author 陈璇
 * @Date 2020/4/16 10:43
 * @Version 1.0
 **/
@RestController
@RequestMapping("app/userInfo")
@Validated
@Api(tags = "用户信息接口", produces = "application/json")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "用户登录", httpMethod = "POST")
    @PostMapping("login")
    public Result login(@ModelAttribute UserInfoVo userInfoVo) throws Exception {
        Map<String, Object> map = userInfoService.login(userInfoVo);
        Integer code = (Integer) map.get("code");
        if(code != null && code.equals(101)){
            String msg = (String) map.get("msg");
            return Result.builder(code, msg)
                    .build();
        }
        UserInfoAndCommunityList getUser = (UserInfoAndCommunityList) map.get("getUser");
        return Result.builder()
                .data(getUser)
                .build();
    }

    @ApiOperation(value = "发送验证码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("sendMessage")
    public Result sendMessage(String phone){
        return userInfoService.sendMessage(phone);
    }

    @ApiOperation(value = "绑定手机号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoId", value = "用户信息ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "验证码",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("bindingMobilePhone")
    public Result bindingMobilePhone(String userInfoId, String phone, String code){
        return userInfoService.bindingMobilePhone(userInfoId, phone, code);
    }

    @ApiOperation(value = "保存居委成员", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "telephone", value = "手机号",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "communityId", value = "社区ID",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("saveCommittee")
    public Result saveCommittee(String name, String telephone, String communityId){
        if(userInfoService.saveCommittee(name, telephone, communityId)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "根据社区ID获取居委ID、名称", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("getCommitteeIdAndName")
    public Result getCommitteeIdAndName(String communityId){
        return Result.builder()
                .data(userInfoService.getCommitteeIdAndName(communityId))
                .build();
    }

    @ApiOperation(value = "根据Id获取用户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户信息ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("getById")
    public Result getById(String id){
        return Result.builder()
                .data(userInfoService.getById(id))
                .build();
    }

    @ApiOperation(value = "根据ID修改手机号和真实姓名", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户信息ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "telephone", value = "手机号",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "realName", value = "真实姓名",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("updateTelephoneAndRealName")
    public Result updateTelephoneAndRealName(String id, String telephone, String realName){
        if(userInfoService.updateTelephoneAndRealName(id, telephone, realName)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

}
